import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'
import { webRoutes } from './webRouter'
import { backStackRoutes } from './backstageRouter'
import { getStorage } from '@/utils'
import { checkToken } from '@/api/user'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/Home'),
    meta: {
      title: '首页'
    },
    children: [...webRoutes, ...backStackRoutes]
  },
  {
    path: '/404',
    name: 'PageNotFound',
    component: () => import('@/views/Exception/404'),
    meta: {
      anonymous: true
    }
  },
  {
    path: '*',
    redirect: '/404'
  },
  {
    path: '/403',
    name: 'PageForbidden',
    component: () => import('@/views/Exception/403'),
    meta: {
      anonymous: true
    }
  },
  {
    path: '/500',
    name: 'PageServiceError',
    component: () => import('@/views/Exception/500'),
    meta: {
      anonymous: true
    }
  },
  
]

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 用户跳转前的路由校验

// 处理路由跳转后的，顶部菜单栏操作
router.beforeEach(async (to, from, next) => {
  const { meta, fullPath, name, query, path } = to
  const { initRouter } = Vue.prototype.$userRouterInfo
  const visitor = getStorage('visitor', 0)
  const token = getStorage('token', 0)
  if (!visitor) {
    // 如果没有游客token说明是首次进来，没有直接拦截掉
    Vue.prototype.$userRouterInfo.truthRouter = path
    next(false)
  }
  const targetBackRouter = backStackRoutes[0].children.find(
    item => item.path === path
  )
  if (targetBackRouter) {
    // 当前跳转的路由属于后台路由
    const { data } = await checkToken(token) // 校验用户token
    if (!data) {
      // 不通过
      Vue.prototype.$apiStatusHandler(
        999,
        '当前角色权限无法进入后台管理,请联系管理员'
      )
      next(initRouter)
    }
  }
  let selectedKeys = []
  if (name === 'generalInfo') {
    const { informationType } = query
    switch (informationType) {
      case '2':
        selectedKeys.push('政策制度')
        break
      case '3':
        selectedKeys.push('产业分析')
        break
      default:
        selectedKeys.push('行业资讯')
        break
    }
  }
  if (process.env.VUE_APP_KEY === 'zhongshan' && meta.title === '服务机构') {
    selectedKeys = ['服务平台']
  } else {
    selectedKeys = meta && meta.title ? meta.title.split(',') : []
  }
  const routerUpdateParams = {
    routerPath: fullPath,
    selectedKeys
  }
  store.commit('headerMenu/updateMenuItemStatus', routerUpdateParams)
  // 处理滚动条回滚到顶部
  const domWrp = document.getElementsByClassName('home-wrap')
  if (domWrp.length > 0) {
    domWrp[0].scrollTop = 0
  }
  next()
})

export default router
